<?php
namespace addons\yalimedia\service;

use app\admin\model\yalimedia\Uservip;
use app\admin\model\yalimedia\Vipcard;

class Order
{

    public $model;

    public function __construct()
    {
        $this->model = new \app\admin\model\yalimedia\Order();
    }

    /**
     * 获取购买记录列表
     * @param $page
     * @param $limit
     * @throws \think\Exception
     */
    public function getOrderList($user_id,$page,$limit){
        $count = $this->model
            ->alias("order")
            ->join("yalimedia_course course","order.item_id=course.id and order.type = 1")
            ->where(['order.user_id'=>$user_id,'order.status'=>2])
            ->group("course.id")
            ->count();
        if($page){
            $data = $this->model
                ->alias("order")
                ->field("course.*")
                ->join("yalimedia_course course","order.item_id=course.id and order.type = 1")
                ->where(['order.user_id'=>$user_id,'order.status'=>2])
                ->page($page,$limit)
                ->group("course.id")
                ->order("weigh asc")->select();
        }else{
            $data = $this->model
                ->alias("order")
                ->field("course.*")
                ->join("yalimedia_course course","order.item_id=course.id and order.type = 1")
                ->where(['order.user_id'=>$user_id,'order.status'=>2])
                ->group("course.id")
                ->order("weigh asc")->select();
        }

        return ['total'=>$count,'data'=>$data];
    }

    /**
     * 支付成功
     */
    public function pay_success($order_no){
        $orderModel = new \app\admin\model\yalimedia\Order();
        if($orderInfo = $orderModel->where(['order_no'=>$order_no,'status'=>1])->find()){
            $orderInfo->save(['status'=>2,'pay_status'=>1,'pay_time'=>time()]);
            if($orderInfo['type'] == 2){
                $vipCardModel = new Vipcard();
                $card = $vipCardModel->where(['id'=>$orderInfo['item_id']])->find();
                if($card){
                    switch ($card['type']){
                        case 1:
                            $expiretime = strtotime("+1 month");
                            break;
                        case 2:
                            $expiretime = strtotime("+3 month");
                            break;
                        case 3:
                            $expiretime = strtotime("+1 year");
                            break;
                        case 4:
                            $expiretime = 0;
                            break;
                    }
                    $userVip = new Uservip();
                    if($userVipInfo = $userVip->where(['user_id'=>$orderInfo['user_id']])->find()){
                        $userVipInfo->save(['expiretime'=>$expiretime]);
                    }else{
                        $userVip = new Uservip();
                        $userVip->save(['user_id'=>$orderInfo['user_id'],'expiretime'=>$expiretime]);
                    }
                }

            }
        }
    }
}